/**
 * Copyright (c) 2006, PORTALIER Julien
 * 
 * Licensed under The LGPL License
 * Redistributions of files must retain the above copyright notice.
 * 
 * @package    Feather-CM
 * @subpackage Javascript
 * @copyright  Copyright (c) 2006, Julien PORTALIER
 * @link       http://portalier.com/feather
 * @license    http://www.opensource.org/licenses/lgpl-license.php The LGPL License
 */
var LicenseWizard = new Class(
{
	initialize: function(select)
	{
		this.select = $(select);
		this.wizard = $('license-wizard');
		
		// selects a license corresponding to selected choices
		$('license-select-cc').addEvent('change', this.onSelect.bindWithEvent(this));
		$('license-select-public').addEvent('change', this.onSelect.bindWithEvent(this));
		$ES('input', 'license-wizard').each(function(input) {
			input.addEvent('change', this.onQuestionAnswer.bindWithEvent(this));
		}, this);
		
		// selects choices corresponding to selected license
		this.select.addEvent('change', this.onLicenseSelected.bind(this));
		this.onLicenseSelected();
	},
	
	disable: function()
	{
		$ES('input', this.wizard).each(function(input) { input.disabled = true; });
	},
	
	enable: function()
	{
		$ES('input', this.wizard).each(function(input) { input.disabled = false; });
	},
	
	onSelect: function(evt)
	{
		var target = evt.target;
		
		if (evt.target.checked && evt.target.id == 'license-select-public')
		{
			this.selectLicense('public');
			this.disable();
		}
		else
		{
			this.onQuestionAnswer();
			this.enable();
		}
	},
	
	onQuestionAnswer: function()
	{
		var commercial = 0, modify = 1;
		
		$ES('input[name=licenseCommercial]', this.wizard).each(function(r)
		{
			if (r.checked)
			{
				commercial = parseInt(r.value);
				$break();
			}
		});
		
		$ES('input[name=licenseModify]', this.wizard).each(function(r)
		{
			if (r.checked)
			{
				modify = parseInt(r.value);
				$break();
			}
		});
		
		switch(modify)
		{
			case 2: this.selectLicense(commercial ? 'cc-by/2.5'    : 'cc-by-nc/2.5');    break;
			case 1: this.selectLicense(commercial ? 'cc-by-sa/2.5' : 'cc-by-nc-sa/2.5'); break;
			case 0: this.selectLicense(commercial ? 'cc-by-nd/2.5' : 'cc-by-nc-nd/2.5');
		}
	},
	
	selectLicense: function(license)
	{
		$ES('option', this.select).each(function(o)
		{
			if (o.value == license)
				o.selected = true;
		});
	},
	
	onLicenseSelected: function()
	{
		if (this.select.value == 'public')
		{
			$('license-select-public').checked = true;
			this.disable();
		}
		else
		{
			$('license-select-cc').checked = true;
			this.enable();
			
			var modify, commercial;
			switch(this.select.value)
			{
				case 'cc-by/2.5':       modify = 2; commercial = 1; break;
				case 'cc-by-sa/2.5':    modify = 1; commercial = 1; break;
				case 'cc-by-nd/2.5':    modify = 0; commercial = 1; break;
				case 'cc-by-nc/2.5':    modify = 2; commercial = 0; break;
				case 'cc-by-nc-sa/2.5': modify = 1; commercial = 0; break;
				case 'cc-by-nc-nd/2.5': modify = 0; commercial = 0;
			}
			
			$ES('input[name=licenseCommercial]', this.wizard).each(function(r)
			{
				if (parseInt(r.value) == commercial)
				{
					r.checked = true;;
					$break();
				}
			});
			
			$ES('input[name=licenseModify]', this.wizard).each(function(r)
			{
				if (parseInt(r.value) == modify)
				{
					r.checked = true;;
					$break();
				}
			});
		}
	}
});